﻿using DongFangGuoKai.TaskPlatform.Commn.Attributes;
using DongFangGuoKai.TaskPlatform.Commn.NoticeModule;
using DongFangGuoKai.TaskPlatform.Domain;
using Quartz;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace DongFangGuoKai.TaskPlatform.Job
{
    public class JobHelper
    {

        /// <summary>
        /// 唯一ID标识
        /// </summary>
        /// <param name="syncIDType"></param>
        /// <returns></returns>
        public static string GetSyncLastId(SyncIDType syncIDType)
        {
            //string id = string.Empty;
            //switch (syncIDType)
            //{
            //    case SyncIDType.InventoryIn:
            //        id = SyncIDType.InventoryIn.ToString();
            //        break;
            //    case SyncIDType.InventoryOut:
            //        id = SyncIDType.InventoryOut.ToString();
            //        break;
            //    case SyncIDType.OutRoundIn:
            //        id = SyncIDType.OutRoundIn.ToString();
            //        break;
            //    case SyncIDType.OutRoundOut:
            //        id = SyncIDType.OutRoundOut.ToString();
            //        break;
            //    case SyncIDType.WarehousingIn:
            //        id = SyncIDType.WarehousingIn.ToString();
            //        break;
            //    case SyncIDType.WarehousingOut:
            //        id = SyncIDType.WarehousingOut.ToString();
            //        break;
            //    default:
            //        break;
            //}
            //return id;   

            return syncIDType.ToString();
        }

        /// <summary>
        /// 海宁后缀Url
        /// </summary>
        /// <param name="syncIDType"></param>
        /// <returns></returns>
        public static string GetHHSuffixUrl(SyncIDType syncIDType)
        {
            string url = string.Empty;
            #region  v1.
            //switch (syncIDType)
            //{
            //    case SyncIDType.InventoryIn:
            //        url = "railway/v1/pda/plan/list";
            //        break;
            //    case SyncIDType.InventoryOut:
            //        url = "railway/v1/pda/plan/action";
            //        break;
            //    case SyncIDType.OutRoundIn:
            //        url = "railway/v1/pda/picking/order/out/list";
            //        break;
            //    case SyncIDType.OutRoundOut:
            //        url = "railway/v1/pda/store/manage/output/add";
            //        break;
            //    case SyncIDType.WarehousingIn:
            //        url = "railway/v1/pda/check/unInput/list";
            //        break;
            //    case SyncIDType.WarehousingOut:
            //        url = "railway/v1/pda/store/manage/input/add";
            //        break;
            //    default:
            //        break;
            //}  
            #endregion

            switch (syncIDType)
            {
                case SyncIDType.InventoryIn:
                    url = "railway/pda/plan/list";
                    break;
                case SyncIDType.InventoryOut:
                    url = "railway/pda/plan/action";
                    break;
                case SyncIDType.OutRoundIn:
                    url = "railway/pda/picking/order/out/list";
                    break;
                case SyncIDType.OutRoundOut:
                    url = "railway/pda/store/manage/output/add";
                    break;
                case SyncIDType.WarehousingIn:
                    url = "railway/pda/check/unInput/list";
                    break;
                case SyncIDType.WarehousingOut:
                    url = "railway/pda/store/manage/input/add";
                    break;
                default:
                    break;
            }
            return url;
        }

        /// <summary>
        /// WMS后缀Url
        /// </summary>
        /// <param name="syncIDType"></param>
        /// <returns></returns>
        public static string GetWMSSuffixUrl(SyncIDType syncIDType)
        {
            string url = string.Empty;
            switch (syncIDType)
            {
                case SyncIDType.InventoryIn:
                    url = "InventoryOrder/AddStocktakingOrder";
                    break;
                case SyncIDType.InventoryOut:
                    url = "InventoryOrder/GetPDAListData";
                    break;
                case SyncIDType.OutRoundIn:
                    url = "TShippingOrderDetail/AddTShippingOrderDetailPlus";
                    break;
                case SyncIDType.OutRoundOut:
                    url = "TShippingOrderDetail/GetOneTShippingOrderDetailPlus";
                    break;
                case SyncIDType.WarehousingIn:
                    url = "TReceivingBill/AddCheck";
                    break;
                case SyncIDType.WarehousingOut:
                    url = "TReceivingBill/GetCheck";
                    break;

                default:
                    break;
            }
            return url;
        }

        /// <summary>
        ///终止Job执行
        /// </summary>
        /// <param name="context">Job上下文</param>
        /// <param name="type">反射类型</param>
        /// <returns></returns>
        public static async Task<bool> StopJob(IJobExecutionContext context, Type type)
        {

            string key = string.Empty;
            string group = string.Empty;
            if (type.IsDefined(typeof(JobAttribute), true))
            {
                JobAttribute attribute = type.GetCustomAttribute<JobAttribute>();
                key = attribute.JobKey;
                group = attribute.JobGroup;
                var scheduler = context.Scheduler;
                JobKey jobKey = new JobKey(key, group);

                //停止Job
                await scheduler.PauseJob(jobKey);
            }

            return true;
        }
        public static async Task<bool> StopJob(IJobExecutionContext context, string group, string key)
        {
            var scheduler = context.Scheduler;
            JobKey jobKey = new JobKey(key, group);

            //停止Job
            await scheduler.PauseJob(jobKey);

            return true;
        }

        /// <summary>
        /// 错误信息发送邮箱
        /// </summary>
        /// <param name="nlogs"></param>
        /// <param name="type"></param>
        public static void SendEmail(List<Nlog> nlogs, string group, string key)
        {
            StringBuilder table = new StringBuilder();
            table.Append($"<h2> {group}_________{key} </h2>");
            table.Append("<br/>");
            table.Append("<table border='1'>  <thead> ");
            table.Append("<tr>");
            table.Append($"<th>Id</th>");
            table.Append($"<th>Logged</th>");
            table.Append($"<th>Level</th>");
            table.Append($"<th>Message</th>");
            table.Append("</tr> </thead>");
            table.Append("<tbody>");
            if (nlogs != null && nlogs.Count > 0)
            {
                foreach (var item in nlogs)
                {
                    table.Append("<tr>");
                    table.Append($"<td>{item.Id} </td>");
                    table.Append($"<td>{item.Logged} </td>");
                    table.Append($"<td>{item.Level} </td>");
                    table.Append($"<td>{item.Message} </td>");
                    table.Append("</tr>");
                }
            }
            table.Append("</tbody>  </table>");

            new SoftMail().SendMail("m19932056122@163.com", "任务调度异常通知",
            new String[] { "1263689354@qq.com", "2323629450@qq.com" },
            "任务调度异常关闭通知", table.ToString(), System.Net.Mail.MailPriority.High,
            true);

        }

        /// <summary>
        ///获取Job组名，JobKey值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static (string, string) GetJobInfo<T>()
        {
            Type type = typeof(T);
            string key = string.Empty;
            string group = string.Empty;
            if (type.IsDefined(typeof(JobAttribute), true))
            {
                JobAttribute attribute = type.GetCustomAttribute<JobAttribute>();
                key = attribute.JobKey;
                group = attribute.JobGroup;
                return (group, key);
            }
            return (string.Empty, string.Empty);
        }
    }

    public enum SyncIDType
    {
        [Description("盘点任务下发")]
        InventoryIn,

        [Description("盘点任务回传")]
        InventoryOut,

        [Description("出库任务下发")]
        OutRoundIn,

        [Description("出库任务回传")]
        OutRoundOut,

        [Description("入库任务下发")]
        WarehousingIn,

        [Description("入库任务回传")]
        WarehousingOut
    }
}
